﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Resource;

namespace EnvironmentalMonitor.Support.Module.Environmental
{
    /// <summary>
    /// 探头类型
    /// </summary>
    public class DetectorType : EnvironmentalMonitor.Support.Module.AbstractModule
    {
        public const int VALUE_LENGTH = 3;

        private string _Name;
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }

        private DetectorTypes _Type;
        /// <summary>
        /// 类型
        /// </summary>
        public DetectorTypes Type
        {
            get
            {
                return this._Type;
            }
            set
            {
                this._Type = value;
            }
        }

        private ushort _Minimum;
        /// <summary>
        /// 最小值
        /// </summary>
        public ushort Minimum
        {
            get
            {
                return this._Minimum;
            }
            set
            {
                this._Minimum = value;
            }
        }

        /// <summary>
        /// 最小值一
        /// </summary>
        public ushort MinimumA
        {
            get
            {
                ushort result = 0;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = this.Minimum;
                        break;
                    case DetectorTypes.Area:
                        result = this.Minimum;
                        break;
                    case DetectorTypes.DoubleArea:
                        result = (ushort)(this.Minimum >> 8);
                        break;
                }

                return result;
            }
        }

        /// <summary>
        /// 最小值二
        /// </summary>
        public ushort MinimumB
        {
            get
            {
                ushort result = 0;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = 0;
                        break;
                    case DetectorTypes.Area:
                        result = 0;
                        break;
                    case DetectorTypes.DoubleArea:
                        result = (ushort)(this.Minimum & 0x00FF);
                        break;
                }

                return result;
            }
        }

        private ushort _Maximum;
        /// <summary>
        /// 最大值
        /// </summary>
        public ushort Maximum
        {
            get
            {
                return this._Maximum;
            }
            set
            {
                this._Maximum = value;
            }
        }

        /// <summary>
        /// 最大值一
        /// </summary>
        public ushort MaximumA
        {
            get
            {
                ushort result = 0;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = this.Maximum;
                        break;
                    case DetectorTypes.Area:
                        result = this.Maximum;
                        break;
                    case DetectorTypes.DoubleArea:
                        result = (ushort)(this.Maximum >> 8);
                        break;
                }

                return result;
            }
        }

        /// <summary>
        /// 最大值二
        /// </summary>
        public ushort MaximumB
        {
            get
            {
                ushort result = 0;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = 0;
                        break;
                    case DetectorTypes.Area:
                        result = 0;
                        break;
                    case DetectorTypes.DoubleArea:
                        result = (ushort)(this.Maximum & 0x00FF);
                        break;
                }

                return result;
            }
        }

        private ushort _Threshold;
        /// <summary>
        /// 阀值
        /// </summary>
        public ushort Threshold
        {
            get
            {
                return this._Threshold;
            }
            set
            {
                this._Threshold = value;
            }
        }

        /// <summary>
        /// 阀值一
        /// </summary>
        public ushort ThresholdA
        {
            get
            {
                ushort result = 0;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = this.Threshold;
                        break;
                    case DetectorTypes.Area:
                        result = this.Threshold;
                        break;
                    case DetectorTypes.DoubleArea:
                        result = (ushort)(this.Threshold >> 8);
                        break;
                }

                return result;
            }
        }

        /// <summary>
        /// 阀值二
        /// </summary>
        public ushort ThresholdB
        {
            get
            {
                ushort result = 0;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = 0;
                        break;
                    case DetectorTypes.Area:
                        result = 0;
                        break;
                    case DetectorTypes.DoubleArea:
                        result = (ushort)(this.Threshold & 0x00FF);
                        break;
                }

                return result;
            }
        }

        private byte _Code;
        /// <summary>
        /// 代码
        /// </summary>
        public byte Code
        {
            get
            {
                return this._Code;
            }
            set
            {
                this._Code = value;
            }
        }

        private string _Normal;
        /// <summary>
        /// 普通图
        /// </summary>
        public string Normal
        {
            get
            {
                return this._Normal;
            }
            set
            {
                this._Normal = value;
            }
        }

        private string _Error;
        /// <summary>
        /// 错误图
        /// </summary>
        public string Error
        {
            get
            {
                return this._Error;
            }
            set
            {
                this._Error = value;
            }
        }

        private string _DescriptionA;
        /// <summary>
        /// 描述A
        /// 正确开关值描述
        /// 范围值描述
        /// 双范围值第一个值描述
        /// </summary>
        public string DescriptionA
        {
            get
            {
                return this._DescriptionA;
            }
            set
            {
                this._DescriptionA = value;
            }
        }

        private string _DescriptionB;
        /// <summary>
        /// 描述B
        /// 错误开关值描述
        /// 
        /// 双范围值第二个值描述
        /// </summary>
        public string DescriptionB
        {
            get
            {
                return this._DescriptionB;
            }
            set
            {
                this._DescriptionB = value;
            }
        }

        /// <summary>
        /// 类型文本
        /// </summary>
        public string TypeText
        {
            get
            {
                string result = string.Empty;

                switch (this.Type)
                {
                    case DetectorTypes.Switch:
                        result = "开关值";
                        break;
                    case DetectorTypes.Area:
                        result = "范围值";
                        break;
                    case DetectorTypes.DoubleArea:
                        result = "双范围值";
                        break;
                }

                return result;
            }
        }

        /// <summary>
        /// 类型值
        /// </summary>
        public int TypeValue
        {
            get
            {
                return (int)this.Type;
            }
        }

        /// <summary>
        /// 普通图链接
        /// </summary>
        public string NormalHref
        {
            get
            {
                return string.Format("<a href=\"{0}{1}\" target=\"_blank\"><img src=\"{0}{1}\" height=\"32\" border=\"0\" /></a>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, this._Normal);
            }
        }

        /// <summary>
        /// 错误图链接
        /// </summary>
        public string ErrorHref
        {
            get
            {
                return string.Format("<a href=\"{0}{1}\" target=\"_blank\"><img src=\"{0}{1}\" height=\"32\" border=\"0\" /></a>", EnvironmentalMonitor.Support.Resource.Variable.VirtualRootPath, this._Error);
            }
        }

        /// <summary>
        /// 编码
        /// </summary>
        /// <returns>数据</returns>
        public byte[] Encode()
        {
            byte[] results = new byte[DetectorType.VALUE_LENGTH];

            switch (this.Type)
            {
                case Resource.DetectorTypes.DoubleArea:
                    byte[] doubleAreavalues = BitConverter.GetBytes(this.Threshold);
                    results[0] = this.Code;
                    results[1] = doubleAreavalues[1];
                    results[2] = doubleAreavalues[0];
                    break;
                case Resource.DetectorTypes.Switch:
                    if (this.Threshold == 0)
                    {
                        results = new byte[] { this.Code, 0x00, 0x00 };
                    }
                    else
                    {
                        results = new byte[] { this.Code, 0x01, 0x01 };
                    }
                    break;
                case Resource.DetectorTypes.Area:
                    byte[] areaValues = BitConverter.GetBytes(this.Threshold);
                    results[0] = this.Code;
                    results[1] = areaValues[1];
                    results[2] = areaValues[0];
                    break;
            }

            return results;
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="values">数据</param>
        public void Decode(byte[] values)
        {
            this.Code = values[0];
            byte[] thresholdValues = { values[2], values[1] };
            switch (this.Type)
            {
                case Resource.DetectorTypes.Area:
                    this.Threshold = BitConverter.ToUInt16(thresholdValues, 0);
                    break;
                case Resource.DetectorTypes.Switch:
                    if (thresholdValues[0] == 0)
                    {
                        this.Threshold = 0;
                    }
                    else
                    {
                        this.Threshold = 1;
                    }
                    break;
            }
        }
    }
}
